var uri = 'http://gupiao.baidu.com/ui/gushitong';
var param = {
  time: {
    cuid: "xxx",
    format: "json",
    from: "pc",
    os_ver: 1,
    queryURL: "stocktimeline",
    stock_code: "sz000001",
    uid: "4f0ba713-f6d5-c66b-0c16-429413cd66ed",
    vv: 100
  },
  fiveday: {
    cuid: "xxx",
    format: "json",
    from: "pc",
    os_ver: 1,
    queryURL: "stocktimelinefive",
    step: 3,
    stock_code: "sz000001",
    uid: "4f0ba713-f6d5-c66b-0c16-429413cd66ed",
    vv: 100
  },
  day: {
    count: 5,
    cuid: "xxx",
    format: "json",
    fq_type: "front", //front,no
    from: "pc",
    os_ver: 1,
    queryURL: "stockdaybar",
    start: "",
    step: "3",
    stock_code: "sz000001",
    uid: "4f0ba713-f6d5-c66b-0c16-429413cd66ed",
    vv: 100
  },
  week: {
    count: 5,
    cuid: "xxx",
    format: "json",
    fq_type: "front",
    from: "pc",
    os_ver: 1,
    queryURL: "stockweekbar",
    start: "",
    step: "3",
    stock_code: "sz000001",
    uid: "4f0ba713-f6d5-c66b-0c16-429413cd66ed",
    vv: 100
  },
  month: {
    count: 5,
    cuid: "xxx",
    format: "json",
    fq_type: "front",
    from: "pc",
    os_ver: 1,
    queryURL: "stockmonthbar",
    start: "",
    step: "3",
    stock_code: "sz000001",
    uid: "4f0ba713-f6d5-c66b-0c16-429413cd66ed",
    vv: 100
  }
};

var t = require('hy5b-t');
var request = require('request');

/**
 * {
  mashData:[
    {
      data: '20151210',
      kline:{
        close:'',
        open:'',
      },
      ma5: {
  
      },
      macd: {
        diff:'',
        dea:'',
        macd:''
      },
      kdj: {
  
      },
      rsi:{
  
      }
    }
  ]
 }
 *
 *
 *
 *
 * 
 */

function formatData(data) {
  var obj = {};
  var amash = data.mashData;
  if (amash) {
    for (var i = 0; i < amash.length; i++) {
      for (var param in amash[i]) {
        if (typeof amash[i][param] != 'object') {
          if (!obj[param])
            obj[param] = [];
          obj[param].push(amash[i][param]);
        } else {
          for (var innerParam in amash[i][param]) {
            if (typeof amash[i][param][innerParam] != 'object') {
              if (!obj[param + innerParam])
                obj[param + innerParam] = [];
              obj[param + innerParam].push(amash[i][param][innerParam]);
            }
          }
        }
      }
    }
  }

  return obj;
}

function queryStock(param, cb) {
  request({
    url: uri + "?" + t.param(param),
    method: "GET" //GET请求不带form
  }, function(error, response, body) {
    if (error) {
      console.log(error);
      cb(null);
    } else {
      try {
        var data = JSON.parse(body);
        cb(formatData(data));
      } catch (e) {
        console.log(e);
        cb(null);
      }
    }
  });
}

module.exports = {
  queryDayLine: function(fullCode, count, cb) {
    queryStock(t.extend({}, param.day, {
      stock_code: fullCode,
      count: count
    }), cb);
  },

  queryWeekLine: function(fullCode, count, cb) {
    queryStock(t.extend({}, param.week, {
      stock_code: fullCode,
      count: count
    }), cb);
  }
};
